package com.onenine.app.view;

import android.content.Context;
import android.os.Handler;
import android.os.Looper;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.annotation.StringRes;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ProgressBar;

import com.onenine.app.R;
import com.onenine.app.utils.DataType;


/**
 * 带有圆形加载的按钮
 */
public class LoadingButton extends FrameLayout implements Runnable {

    private static Handler sHandler = new Handler(Looper.myLooper());
    private Button mBtnOpt;
    private ProgressBar mPbLoading;
    private String mBtnText;

    public LoadingButton(@NonNull Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public LoadingButton(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initLayoutView(context);
    }

    /**
     * 初始化
     *
     * @param context
     */
    private void initLayoutView(Context context) {
        LayoutInflater.from(context).inflate(R.layout.layout_loading_button, this, true);
        mBtnOpt = findViewById(R.id.btn_opt);
        mPbLoading = findViewById(R.id.pb_loading);
    }

    /**
     * 设置按钮文本
     *
     * @param text
     */
    public void setButtonText(String text) {
        mBtnOpt.setText(text);
        mBtnText = text;
    }

    /**
     * 设置按钮文本
     *
     * @param resid
     */
    public void setButtonText(@StringRes int resid) {
        mBtnOpt.setText(resid);
        mBtnText = mBtnOpt.getText().toString();
    }

    /**
     * 设置背景样式
     *
     * @param sex
     */
    public void setButtonStyle(int sex) {
        if (sex == DataType.SEX_MALE) {
            mBtnOpt.setBackgroundResource(R.drawable.selector_corner_blue_enable_bg);
        } else {
            mBtnOpt.setBackgroundResource(R.drawable.selector_corner_pink_enable_bg);
        }
    }

    /**
     * 设置按钮可用性
     *
     * @param enable
     */
    public void setButtonEnable(boolean enable) {
        mBtnOpt.setEnabled(enable);
        mPbLoading.setVisibility(View.GONE);
    }

    /**
     * 设置按钮点击事件监听
     *
     * @param l
     */
    public void setButtonOnClickListener(@Nullable OnClickListener l) {
        mBtnOpt.setOnClickListener(l);
    }

    /**
     * 设置加载中
     *
     * @param isLoading
     */
    public void setLoading(boolean isLoading) {
        if (isLoading) {
            mPbLoading.setVisibility(View.VISIBLE);
            mBtnOpt.setClickable(false);
            mBtnOpt.setText("");
        } else {
            sHandler.postDelayed(this, 500);
        }
    }

    /**
     * 是否处于加载中
     *
     * @return
     */
    public boolean isLoading() {
        return mPbLoading.getVisibility() == View.VISIBLE;
    }

    @Override
    public void run() {
        mPbLoading.setVisibility(View.GONE);
        mBtnOpt.setClickable(true);
        mBtnOpt.setText(mBtnText);
    }

}
